package com.xmrecruit.util

import android.content.Context
import android.content.SharedPreferences
import android.content.SharedPreferences.Editor
import com.xmrecruit.CC

/**
 * 首�?项工具类

 * @author cstdingran@gmail.com
 */
object PreferenceUtil  {
    private val PREFERENCE_NAME = "preference"

    private var sp: SharedPreferences? = null

    private var ed: Editor? = null


     init{
        if (sp == null || ed == null) {
            try {
                sp = CC.applicationContext?.getSharedPreferences(PREFERENCE_NAME, 0)
                ed = sp!!.edit()
            } catch (e: Exception) {
            }

        }
    }

    fun saveLong(key: String, l: Long) {
        ed!!.putLong(key, l)
        ed!!.commit()
    }

    fun getLong(key: String, defaultlong: Long): Long {
        return sp!!.getLong(key, defaultlong)
    }

    fun saveBoolean(key: String, value: Boolean) {
        ed!!.putBoolean(key, value)
        ed!!.commit()
    }

    fun getBoolean(key: String, defaultboolean: Boolean): Boolean {
        return sp!!.getBoolean(key, defaultboolean)
    }

    fun saveInt(key: String, value: Int) {
        if (ed != null) {
            ed!!.putInt(key, value)
            ed!!.commit()
        }
    }

    fun getInt(key: String, defaultInt: Int): Int {
        return sp!!.getInt(key, defaultInt)
    }

    fun getString(key: String, defaultInt: String): String {
        return sp!!.getString(key, defaultInt)
    }

    fun getString(context: Context, key: String, defaultValue: String): String {
        if (sp == null || ed == null) {
            sp = context.getSharedPreferences(PREFERENCE_NAME, 0)
            ed = sp!!.edit()
        }
        if (sp != null) {
            return sp!!.getString(key, defaultValue)
        }
        return defaultValue
    }


    fun saveString(key: String, value: String) {
        ed!!.putString(key, value)
        ed!!.commit()
    }

    fun remove(key: String) {
        ed!!.remove(key)
        ed!!.commit()
    }

    fun destroy() {
        sp = null
        ed = null
    }

}
